package com.goods.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.goods.business.mapper.*;
import com.goods.common.error.SystemException;
import com.goods.common.model.business.*;
import com.goods.common.vo.business.InStockItemVO;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.UserInfoVO;
import com.goods.service.InStockService;
import com.goods.system.service.UserService;
import com.goods.system.util.MD5Utils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class InStockServiceImpl implements InStockService {

    @Autowired
    InStockMapper inStockMapper;

    @Autowired
    SupplierMapper supplierMapper;

    @Autowired
    InStockInfoMapper inStockInfoMapper;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    ProductStockMapper productStockMapper;

    @Autowired
    UserService userService;

    @Override
    public List<InStock> findInStockList(Integer status) {
        InStock inStock = new InStock();
        inStock.setStatus(status);
        return inStockMapper.select(inStock);
    }

    @Override
    public List<InStockItemVO> detail(Long id, Integer pageNum) {
        List<InStockItemVO> list =inStockMapper.selectInStockItemVO(id);
        return list;
    }

    @Override
    public InStock selectInStock(Long id) {
        InStock inStock = new InStock();
        inStock.setId(id);
        return inStockMapper.selectOne(inStock);
    }

    @Override
    public SupplierVO selectSupplierVO(Long supplierId) {
        Supplier supplier = new Supplier();
        supplier.setId(supplierId);
        Supplier supplier1 = supplierMapper.selectOne(supplier);
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(supplier1,supplierVO);
        return supplierVO;
    }

    @Override
    public List<InStock> findInStockList(Integer status, Integer type, String inNum, String startTime, String endTime) {
        return inStockMapper.selectInstockByAny(status,type,inNum,startTime,endTime);
    }

    @Override
    public Supplier findSupplier(Long supplierId) {
        Supplier supplier = new Supplier();
        supplier.setId(supplierId);
        return supplierMapper.selectOne(supplier);
    }

    @Override
    public int remove(Long id) {
        InStock inStock = new InStock();
        inStock.setId(id);
        InStock inStock1 = inStockMapper.selectOne(inStock);
        inStock1.setStatus(1);
        return inStockMapper.updateByPrimaryKey(inStock1);
    }

    @Override
    public int back(Long id) {
        InStock inStock = new InStock();
        inStock.setId(id);
        InStock inStock1 = inStockMapper.selectOne(inStock);
        inStock1.setStatus(0);
        return inStockMapper.updateByPrimaryKey(inStock1);
    }

    @Override
    public int delete(Long id) {
        InStock inStock = new InStock();
        inStock.setId(id);
        return inStockMapper.delete(inStock);
    }

    @Override
    public int addIntoStock(InStockVO inStockVO) {
        InStock inStock = new InStock();

        inStock.setSupplierId(inStockVO.getSupplierId());
        if (inStockVO.getSupplierId() == null){
            Supplier supplier = new Supplier();
            //    private String name;
            supplier.setName(inStockVO.getName());
            //    private String address;
            supplier.setAddress(inStockVO.getAddress());
            //    private String email;
            supplier.setEmail(inStockVO.getEmail());
            //    private String phone;
            supplier.setPhone(inStockVO.getPhone());
            //    private Integer sort;
            supplier.setSort(inStockVO.getSort());
            //    private String contact;
            supplier.setContact(inStockVO.getContact());
            //private Date createTime;
            supplier.setCreateTime(new Date());
            //    private Date modifiedTime;
            supplier.setModifiedTime(new Date());

            supplierMapper.insert(supplier);
            //Supplier supplier1 = supplierMapper.selectOne(supplier);
            inStock.setSupplierId(supplier.getId());
        }
        //    private Long id;

        //    private String inNum;
        inStock.setInNum(UUID.randomUUID().toString().replaceAll("-",""));
        //    private Integer type;
        inStock.setType(inStockVO.getType());
        //    private String operator;
        try {
            UserInfoVO info = userService.info();
            inStock.setOperator(info.getUsername());
        } catch (SystemException e) {
            e.printStackTrace();
        }

        //    private Long supplierId;

        //    private Date createTime;
        inStock.setCreateTime(new Date());
        //    private Date modified;
        inStock.setModified(new Date());
        //    private Integer productNumber;
        List<Object> products = inStockVO.getProducts();
        int i = 0;
        for (Object product : products) {
            HashMap hashMap = JSONObject.parseObject(JSONObject.toJSONString(product), HashMap.class);
            Integer productNumber = (Integer) hashMap.get("productNumber");
            i += productNumber;
            Integer productId = (Integer) hashMap.get("productId");
            Product product1 = new Product();
            product1.setId(Long.parseLong(productId.toString()));
            Product product2 = productMapper.selectOne(product1);
            String pNum = product2.getPNum();
            //中间表增加数据
            InStockInfo inStockInfo = new InStockInfo();
            inStockInfo.setInNum(inStock.getInNum());
            inStockInfo.setPNum(pNum);
            inStockInfo.setCreateTime(new Date());
            inStockInfo.setModifiedTime(new Date());
            inStockInfo.setProductNumber(productNumber);
            inStockInfoMapper.insert(inStockInfo);
        }
        inStock.setProductNumber(i);
        //    private String remark;
        inStock.setRemark(inStockVO.getRemark());
        //    private Integer status;
        inStock.setStatus(2);

        return inStockMapper.insert(inStock);
    }

    @Override
    public int publish(Long id) {
        InStock inStock = new InStock();
        inStock.setId(id);
        InStock inStock1 = inStockMapper.selectOne(inStock);

        InStockInfo inStockInfo = new InStockInfo();
        inStockInfo.setInNum(inStock1.getInNum());
        List<InStockInfo> inStockInfo1 = inStockInfoMapper.select(inStockInfo);
        for (InStockInfo stockInfo : inStockInfo1) {
            Integer productNumber = stockInfo.getProductNumber();
            ProductStock productStock = new ProductStock();
            productStock.setPNum(stockInfo.getPNum());
            ProductStock productStock1 = productStockMapper.selectOne(productStock);
            productStock1.setStock(productStock1.getStock() + productNumber);
            productStockMapper.updateByPrimaryKey(productStock1);
        }


        inStock1.setStatus(0);
        return inStockMapper.updateByPrimaryKey(inStock1);
    }

}
